"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileHasChangeFromCommit = exports.getHeadId = void 0;
const simplegit = require('simple-git/promise');
let git;
/**
 * 获取 git head commitId
 */
function getHeadId() {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            git = git || simplegit();
            const headCommit = yield git.log({
                n: 1,
            });
            const hash = `${headCommit.latest.hash}`;
            return hash;
        }
        catch (_a) {
            return '';
        }
    });
}
exports.getHeadId = getHeadId;
/**
 * 判断某个文件距离某个提交版本是否发送变化
 * @param {string} [partten] 需要和当前head对比的 commitId
 * @param {string} [commitId] 需要和当前head对比的 commitId
 * @returns {boolean} 是否变化
 */
function fileHasChangeFromCommit(partten, commitId) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            git = git || simplegit();
            const headCommitId = yield getHeadId();
            if (headCommitId === commitId) {
                return false;
            }
            const diffResult = yield git.diffSummary([commitId, headCommitId]);
            let isChange = false;
            if (typeof partten === 'string') {
                isChange = diffResult.files.some((diffItem) => {
                    return diffItem.file.includes(partten);
                });
            }
            if (partten instanceof RegExp) {
                isChange = diffResult.files.some((diffItem) => {
                    return partten.test(diffItem.file);
                });
            }
            return isChange;
        }
        catch (_a) {
            return true;
        }
    });
}
exports.fileHasChangeFromCommit = fileHasChangeFromCommit;
